
<?
	
	$k = 0;
	$grouped_grids = array();

	foreach($holiday_search_response as $value){
	
		// GRD result
		if($value[1] == 'GRD'){
		
			if(1==1){
	
				// Get all prices
				$prices = array('AN' => $value[11],
				'AB' => $value[12],
				'RO' => $value[13],
				'SC' => $value[14],
				'BB' => $value[15],
				'HB' => $value[16],
				'FB' => $value[17],
				'FI' => $value[18],
				'AI' => $value[19],
				'2S' => $value[20],
				'4S' => $value[21]);
				// Get unique prices
				$unique_prices = array_unique($prices);
				// Sort the vlaues lowest > highest
				asort($unique_prices);
				// Remove prices at zero
				$filtered_prices = array_filter($unique_prices, "remove_zeros");
				// Get lowest price
				$lowest_price = current($filtered_prices);
				// Get the key of lowest price
				$lowest_bb = key($filtered_prices);
				
				// Apply the lowest price and bb to an array
				if($lowest_price AND $value[9] AND $value[6] AND $value[8]){
			
					$grouped_grids[$k] = array('departure_date' => $value[7],
					'tour_op' => $value[6],
					'destination_airport' => $value[9],
					'departure_airport' => $value[8],
					'nights' => $value[10],
					'lowest_bb' => $lowest_bb,
					'lowest_price' => $lowest_price);
			
					$k++;
					
				}
			}
		}
	}
	

	
	$regrouped_grids = array();
	$regrouped_grids_compare = array();
	
	
	foreach($grouped_grids as $value){
	
		$value_compare = array('departure_date' => $value['departure_date'],
		'destination_airport' => $value['destination_airport'],
		'departure_airport' => $value['departure_airport'],
		'nights' => $value['nights']);
		
		if(!$regrouped_grids_compare) $regrouped_grids_compare[0] = $value_compare;
		if(!$regrouped_grids) $regrouped_grids[0] = $value;
		$difference = true;
		
		for($l=0;$l < count($regrouped_grids_compare); $l++){ // compare the new one to the existing
		
			if(!array_diff($regrouped_grids_compare[$l], $value_compare)){ // if same
				$difference = false;	
				
				
				if($regrouped_grids[$l]['lowest_price'] > $value['lowest_price']){
					$regrouped_grids[$l]['lowest_price'] = $value['lowest_price'];
					$regrouped_grids[$l]['lowest_bb'] = $value['lowest_bb'];
					$regrouped_grids[$l]['tour_op'] = $value['tour_op'];
				}
				
				
			}
			
		}


		if($difference){
			array_push($regrouped_grids_compare, $value_compare);
			array_push($regrouped_grids, $value);
		}
	
	}
	
// Declare array
$results = array();

foreach($regrouped_grids as $value){


	$filter = false;
	
	// Filters
	// Filter UK Operators
	if(in_array($value['departure_airport'], $holidays->north)){
		if(!in_array($value['tour_op'],$holidays->active_ni_tos)) $filter = true;	
	}
	
	// If Destination Airport is not in the DB
	
	// Get destination using airport code
	$params['airport_code'] = $value['destination_airport'];
	$params['type_id'] = '1';
	$params['position'] = 'bottom';
	$params['cache']['seconds'] = '2400';
	$destination_values = $destinations->destination_lookup($params);
	unset($params);
	
	if(!$destination_values) $filter = true;	
	
	// If Destination Airport is a ski airport and its summer now
	if($holidays->ski_destination_airport_filter($value['destination_airport'], $value['departure_date'])) $filter = true;	
	
	// Filer the destinaion name is it contains the string "(ski)"
	//$destination_name = $destinations->ski_destination_name_filter($destination_values[0]['name'], $value['departure_date']);
	$destination_name = $destination_values[0]['name'];
	

if(!$filter){

	$value['pax_adult'] = $holiday_search_params['pax_adult'];
	$value['pax_child'] = $holiday_search_params['pax_child'];
	$url_string = $lurls->holiday_list_lurl($value);
	
	// Get departure airport
	$params['airport_code'] = $value['departure_airport'];
	$departure_airport = $airports->get_airport($params);
	unset($params);
	
	// Get price
	unset($display_price);
	$params['price'] = $value['lowest_price'];
	$params['departure_airport'] = $value['departure_airport'];
	$params['tour_op'] = $value['tour_op'];
	//print_r($params);
	$display_price = $holidays->display_price($params);
	unset($params);
	
	
	// Get link title
	$item_title = "Holiday to ".$destination_name." from ".$departure_airport[0]['simple'];
	
	$prepared_array = array(
							'item_title' => $item_title,
							'url_string' => $url_string,
							'departure_date' => $value['departure_date'],
							'destination_name' => $destination_name,
							'departure_airport_simple' => $departure_airport[0]['simple'],
							'nights' => $value['nights'],
							'display_price' => $display_price
							);

	$results[] = $prepared_array;
	
	}

}




if($results){
?>

<div class="results_head_div">
    <table>
        <tr>
            <td width="140"><strong>Departure date</strong></td>
            <td width="160"><strong>Destination</strong></td>
            <td width="130"><strong>Departing from</strong></td>
            <td width="70"><strong>Nights</strong></td>
            <td width="90" class="a_right"><strong>Prices from</strong></td>
        </tr>
    </table>
</div>


<?
$p = 0;
foreach($results as $result){
?>

<div class="lowlight results_body_div" onclick="location='<?= $result['url_string'] ?>'"  onmouseover="this.className='highlight results_body_div';" onmouseout="this.className='lowlight results_body_div';">


<table class="results_body_table">
		<tr>
		  <td width="140"><img src="/images/pixel.gif" alt="" width="140"  height="1" /></td>
			<td width="160"><img src="/images/pixel.gif" alt="" width="160"  height="1" /></td>
			<td width="130"><img src="/images/pixel.gif" alt="" width="130"  height="1" /></td>
			<td width="70"><img src="/images/pixel.gif" alt="" width="70"  height="1" /></td>
		  <td width="90"><img src="/images/pixel.gif" alt="" width="90"  height="1" /></td>
		</tr>
		<tr>
			<td><?
			
			if($result['departure_date'] == $results[$p-1]['departure_date']){
				
			}else{
				echo display_sabs_date($result['departure_date'],'l j F');
			}
			
			
			?>
			</td>
			<td><strong><?= $result['destination_name']; ?></strong></td>
			<td><?= $result['departure_airport_simple']; ?></td>
			<td><?= $result['nights'] ?> Nights</td>
			<td class="a_right"><a href="<?= $result['url_string']  ?>" class="price_big" title="<?= $result['item_title'] ?>" >&euro;<?= $result['display_price']; ?></a> <span class="price_asterix">*</span></td>
		</tr>
</table>

</div>



<?
		
		
	$p++;
}

}
?>



<?
	// If no results
	if($k == 0){

?>

<img class="photo" src="/images/random_beachball.jpg" alt=""/>

<p>Sorry but we couldn't find any holidays that matched your search. Please try a <a href="/holidays/index.html">new search</a>.</p>

<script type="text/javascript"><!--

google_ad_client = "pub-0942262475991955";

/* 468x60, created 25/11/08 */

google_ad_slot = "8640375771";

google_ad_width = 468;

google_ad_height = 60;

//-->

</script>

<script type="text/javascript"

src="http://pagead2.googlesyndication.com/pagead/show_ads.js">

</script>

<? }else{ ?>


<br />

<p class="a_right"><a href="javascript:history.go(-1)" class="back">&#8249; Back</a> | <a href="/holidays/index.html" class="back">New search</a></p>

<p class="no_margin">* Prices include <strong>Flights, Transfers</strong> and <strong>Accommodation</strong>. Prices are per person and are based on the maximum number of people occupying the accommodation or per accommodation. Final pricing will be confirmed at time of booking as supplements may apply.</p>


<? } ?>

